/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  com.github.pagehelper.PageHelper
 *  de.felixroske.jfxsupport.FXMLController
 *  de.felixroske.jfxsupport.GUIState
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.Parent
 *  javafx.scene.Scene
 *  javafx.scene.control.Button
 *  javafx.scene.control.CheckBox
 *  javafx.scene.control.Pagination
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.HBox
 *  javafx.scene.text.Text
 *  javafx.scene.web.WebEngine
 *  javafx.scene.web.WebView
 *  javafx.stage.FileChooser
 *  javafx.stage.FileChooser$ExtensionFilter
 *  javafx.stage.Modality
 *  javafx.stage.Stage
 *  javafx.stage.Window
 *  javafx.util.Callback
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.CollectionUtils
 */
package com.gys.controller.example;

import com.github.pagehelper.PageHelper;
import com.gys.GysApplication;
import com.gys.common.data.PageInfo;
import com.gys.common.util.ConstUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.example.EditProductController;
import com.gys.controller.index.IndexController;
import com.gys.controller.media.SimpleMediaPlayer;
import com.gys.data.example.GetProductBaseInData;
import com.gys.data.example.GetProductOutData;
import com.gys.data.example.GetUserOutData;
import com.gys.view.example.EditProductView;
import com.gys.view.example.ExampleSubView;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.util.Callback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

@FXMLController
public class ExampleController
extends BaseController
implements Initializable {
    @FXML
    private Text indexText;
    @FXML
    private TableView<GetProductOutData> productTable;
    @FXML
    private TableColumn<GetProductOutData, CheckBox> commonCheckBox;
    @FXML
    private TableColumn<GetProductOutData, Long> id;
    @FXML
    private TableColumn<GetProductOutData, String> productName;
    @FXML
    private TableColumn<GetProductOutData, Integer> productNum;
    @FXML
    private TableColumn<GetProductOutData, String> editProduct;
    @FXML
    private TableColumn<GetProductOutData, String> deleteProduct;
    @FXML
    private Pagination pagination;
    @FXML
    private HBox hb;
    @Autowired
    private IndexController indexController;
    private final int pageSize = ConstUtil.DEFAULT_PAGESIZE;
    private final int defaultPageCount = ConstUtil.DEFAULT_PAGECOUNT;
    private Button editButton;
    private Button deleteButton;
    protected GetProductOutData editProductData;
    protected GetProductOutData deleteProductData;
    private ObservableList<GetProductOutData> ObservableProductTableList = FXCollections.observableArrayList(new ArrayList());

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        GetUserOutData user = this.getLoginUser();
        this.indexText.setText("\u767b\u5f55\u6210\u529f\uff0c\u767b\u5f55\u8d26\u53f7\u662f\uff1a" + (user == null ? "" : user.getAccount()));
        this.pagination.setPageFactory((Callback)new Callback<Integer, Node>(){

            public Node call(Integer pageNum) {
                return ExampleController.this.initTable(pageNum);
            }
        });
        this.indexController.scrollContentPane.setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

            public void handle(KeyEvent e) {
                if (e.getCode() == KeyCode.ENTER) {
                    System.out.println(1111);
                    e.consume();
                }
            }
        });
    }

    private TableView<GetProductOutData> initTable(int pageNum) {
        GetProductBaseInData inData = new GetProductBaseInData();
        inData.setPageNum(pageNum + 1);
        inData.setPageSize(this.pageSize);
        PageInfo<GetProductOutData> pageInfo = this.findByPage(inData);
        List<GetProductOutData> productList = null;
        if (pageInfo != null && !CollectionUtils.isEmpty(pageInfo.getList())) {
            this.pagination.setPageCount(pageInfo.getPages());
            productList = pageInfo.getList();
        } else {
            this.pagination.setPageCount(this.defaultPageCount);
            productList = new ArrayList();
        }
        this.ObservableProductTableList.clear();
        this.ObservableProductTableList = FXCollections.observableArrayList(productList);
        this.commonCheckBox.setCellValueFactory(cellData -> ((GetProductOutData)cellData.getValue()).getCommonCheckBox().getCustomCheckBox());
        this.id.setCellValueFactory((Callback)new PropertyValueFactory("id"));
        this.productName.setCellValueFactory((Callback)new PropertyValueFactory("productName"));
        this.productNum.setCellValueFactory((Callback)new PropertyValueFactory("productNum"));
        this.editProduct.setCellFactory(col -> {
            TableCell<GetProductOutData, String> cell = new TableCell<GetProductOutData, String>(){

                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    ExampleController.this.editButton = new Button("\u7f16\u8f91");
                    ExampleController.this.editButton.setStyle("-fx-background-color: #00bcff;-fx-text-fill: #ffffff");
                    ExampleController.this.editButton.setOnMouseClicked(col -> {
                        ExampleController.this.editProductData = (GetProductOutData)ExampleController.this.ObservableProductTableList.get(this.getIndex());
                        EditProductController editProductController = (EditProductController)ControllerManger.controllerMap.get(EditProductController.class.getSimpleName());
                        if (editProductController != null) {
                            editProductController.initForm();
                        }
                        GysApplication.showView(EditProductView.class, (Modality)Modality.APPLICATION_MODAL);
                    });
                    if (empty) {
                        this.setText(null);
                        this.setGraphic(null);
                    } else {
                        this.setGraphic((Node)ExampleController.this.editButton);
                    }
                }
            };
            return cell;
        });
        this.deleteProduct.setCellFactory(col -> {
            TableCell<GetProductOutData, String> cell = new TableCell<GetProductOutData, String>(){

                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    ExampleController.this.deleteButton = new Button("\u5220\u9664");
                    ExampleController.this.deleteButton.setStyle("-fx-background-color: #00bcff;-fx-text-fill: #ffffff");
                    ExampleController.this.deleteButton.setOnMouseClicked(col -> {
                        ExampleController.this.deleteProductData = (GetProductOutData)ExampleController.this.ObservableProductTableList.get(this.getIndex());
                        if (ExampleController.this.deleteProductData != null) {
                            ExampleController.this.deleteProducts(Arrays.asList(ExampleController.this.deleteProductData.getId()));
                            ExampleController.this.refreshTable();
                        }
                    });
                    if (empty) {
                        this.setText(null);
                        this.setGraphic(null);
                    } else {
                        this.setGraphic((Node)ExampleController.this.deleteButton);
                    }
                }
            };
            return cell;
        });
        this.productTable.setItems(this.ObservableProductTableList);
        return this.productTable;
    }

    public PageInfo<GetProductOutData> findByPage(GetProductBaseInData inData) {
        PageHelper.startPage((int)inData.getPageNum(), (int)inData.getPageSize());
        ArrayList outData = new ArrayList();
        PageInfo<GetProductOutData> pageInfo = CollectionUtils.isEmpty(outData) ? new PageInfo(new ArrayList()) : new PageInfo<GetProductOutData>(outData);
        return pageInfo;
    }

    protected void refreshTable() {
        this.initTable(0);
        this.pagination.setCurrentPageIndex(0);
    }

    @FXML
    private void addProduct() {
        GetProductBaseInData inData = new GetProductBaseInData();
        inData.setProductName("\u65b0\u5546\u54c1" + System.currentTimeMillis());
        inData.setProductNum(5);
        this.refreshTable();
    }

    @FXML
    private void batchDeleteProduct() {
        ArrayList<Long> ids = new ArrayList<Long>();
        if (!CollectionUtils.isEmpty(this.ObservableProductTableList)) {
            for (GetProductOutData i : this.ObservableProductTableList) {
                if (!i.getCommonCheckBox().isSelected().booleanValue()) continue;
                ids.add(i.getId());
            }
        }
        this.deleteProducts(ids);
        this.refreshTable();
    }

    private void deleteProducts(List<Long> ids) {
        GetProductBaseInData deleteInData = new GetProductBaseInData();
        deleteInData.setIds(ids);
    }

    @FXML
    private void selectAll() {
        this.selectAllOrCancelSelectAll(true);
    }

    @FXML
    private void cancelSelectAll() {
        this.selectAllOrCancelSelectAll(false);
    }

    private void selectAllOrCancelSelectAll(Boolean status) {
        if (!CollectionUtils.isEmpty(this.ObservableProductTableList)) {
            for (GetProductOutData i : this.ObservableProductTableList) {
                i.getCommonCheckBox().setSelectStatus(status);
            }
        }
    }

    @FXML
    private void keyEnterEvent() {
        System.out.println(1111);
    }

    @FXML
    private void openNextPage() {
        GysApplication.showView(ExampleSubView.class);
    }

    @FXML
    private void openOuterWeb() {
        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();
        webEngine.load("https://www.baidu.com");
        Stage newStage = new Stage();
        Scene newScene = browser.getScene() != null ? browser.getScene() : new Scene((Parent)browser);
        newStage.setScene(newScene);
        newStage.initModality(Modality.APPLICATION_MODAL);
        newStage.initOwner((Window)GUIState.getStage());
        newStage.showAndWait();
    }

    @FXML
    private void openInnerWeb() {
        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();
        webEngine.load(this.getClass().getResource("/html/Page1.html").toExternalForm());
        GUIState.getScene().setRoot((Parent)browser);
    }

    @FXML
    private void openMedia() {
        SimpleMediaPlayer.popup(this.getClass().getResource("/media/oow2010-2.flv").toString());
    }

    @FXML
    private void importFile() {
        Stage stage = new Stage();
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("\u5bfc\u5165");
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter[]{new FileChooser.ExtensionFilter("PNG files (*.png)", new String[]{"*.png"}), new FileChooser.ExtensionFilter("Allfiles", new String[]{"*.*"})});
        File target = fileChooser.showOpenDialog((Window)stage);
        if (target == null) {
            return;
        }
        System.out.println(target);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @FXML
    private void exportFile() {
        Stage stage = new Stage();
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("\u5bfc\u51fa");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
        fileChooser.setInitialFileName("123.png");
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter[]{new FileChooser.ExtensionFilter("PNG files (*.png)", new String[]{"*.png"}), new FileChooser.ExtensionFilter("Allfiles", new String[]{"*.*"})});
        File target = fileChooser.showSaveDialog((Window)stage);
        if (target == null) {
            return;
        }
        InputStream input = this.getClass().getResourceAsStream("/image/mediaIcon/max.png");
        FileOutputStream output = null;
        try {
            int bytesRead;
            output = new FileOutputStream(target);
            byte[] buf = new byte[1024];
            while ((bytesRead = input.read(buf)) != -1) {
                ((OutputStream)output).write(buf, 0, bytesRead);
            }
            output.flush();
            ((OutputStream)output).close();
            input.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (input != null) {
                    input.close();
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (output != null) {
                    ((OutputStream)output).close();
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

